package com.ptswitch.material.pages.contract;

import java.util.ArrayList;
import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.ptswitch.material.model.contract.Contract;
import com.ptswitch.material.model.contract.ContractItem;
import com.ptswitch.material.pages.base.PageBase;
import com.ptswitch.material.service.ContractService;
import com.ptswitch.material.util.security.PermissionUtil;

public class ViewItems extends PageBase {
    @Inject
    private ContractService contractSer;
    @Property
    private long id;
    @Property
    private List<ContractItem> receivingButNotPassItems;
    @Property
    private List<ContractItem> receivingAndPassItems;
    @Property
    private List<ContractItem> unReceivingItems;

    Object withInit(long id) {
        this.id = id;
        return this;
    }

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(long id) {
        checkPermitted(PermissionUtil.ContractItemList);
        withInit(id);
        // init for pager
        Contract contract = contractSer.findById(id, ContractService.Init_Items);
        unReceivingItems = new ArrayList<ContractItem>();
        receivingAndPassItems = new ArrayList<ContractItem>();
        receivingButNotPassItems = new ArrayList<ContractItem>();
        for (ContractItem item : contract.getContractItems()) {
            if (!item.isReceivingOk()) {
                unReceivingItems.add(item);
            } else {
                if (item.isPassOk()) {
                    receivingAndPassItems.add(item);
                } else {
                    receivingButNotPassItems.add(item);
                }
            }
        }
        return null;
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return id;
    }

}
